package top.dreamcenter.ai.llm.llmchat.config;

import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileUrlResource;
import top.dreamcenter.ai.llm.llmchat.LlmChatApplication;
import top.dreamcenter.ai.common.constant.CommonConstant;

import java.net.URL;

@Configuration
public class BeanRegisterConfig {

    @Bean
    public SimpleVectorStore vectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(embeddingModel).build();

        URL resource = LlmChatApplication.class.getClassLoader().getResource(CommonConstant.SIMPLE_EMBEDDING_RESULT_FILE);

        if (resource != null) {
            simpleVectorStore.load(new FileUrlResource(resource));
        }

        return simpleVectorStore;
    }

    @Bean
    public InMemoryChatMemory inMemoryChatMemory() {
        return new InMemoryChatMemory();
    }
}
